package com.example.aijiancece.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.aijiancece.R;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;

public class DetectFragment extends Fragment {

    private TextInputEditText etUrl;
    private TextInputEditText etText;
    private final ActivityResultLauncher<String[]> fileChooser = registerForActivityResult(
            new ActivityResultContracts.OpenDocument(),
            uri -> {
                if (uri != null) {
                    checkDocument(uri);
                }
            }
    );

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_detect, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        etUrl = view.findViewById(R.id.etUrl);
        etText = view.findViewById(R.id.etText);
        MaterialButton btnCheckUrl = view.findViewById(R.id.btnCheckUrl);
        MaterialButton btnCheckText = view.findViewById(R.id.btnCheckText);
        MaterialButton btnUpload = view.findViewById(R.id.btnUpload);

        btnCheckUrl.setOnClickListener(v -> checkUrl());
        btnCheckText.setOnClickListener(v -> checkText());
        btnUpload.setOnClickListener(v -> openFileChooser());
    }

    private void checkUrl() {
        String url = etUrl.getText().toString();
        if (url.isEmpty()) {
            Toast.makeText(requireContext(), "请输入链接", Toast.LENGTH_SHORT).show();
            return;
        }
        // TODO: 实现URL检测逻辑
    }

    private void checkText() {
        String text = etText.getText().toString();
        if (text.isEmpty()) {
            Toast.makeText(requireContext(), "请输入文本", Toast.LENGTH_SHORT).show();
            return;
        }
        // TODO: 实现文本检测逻辑
    }

    private void openFileChooser() {
        fileChooser.launch(new String[]{"text/*", "application/pdf"});
    }

    private void checkDocument(Uri uri) {
        // TODO: 实现文档检测逻辑
    }
} 